package com.zez.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zez.common.util.PageResult;
import com.zez.entity.Classes;
import com.zez.entity.Student;
import com.zez.mapper.StudentMapper;
import com.zez.service.IClassesService;
import com.zez.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zez.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zez
 * @since 2025-01-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private IClassesService classesService;

    @Override
    public PageResult<Student> findbypage (Integer page, Integer pagesize) {
        Page<Student> page1 = this.page(new Page<>(page, pagesize));// 分页查询
        return new PageResult<>(page1.getRecords(), page1.getTotal());
    }
    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVO vo) {
        //  封装查询条件
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getSname()), "sname", vo.getSname());
        wrapper.like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr());
        wrapper.eq(false, "cid", vo.getCid());

        Page<Student> page1 = this.page(new Page<>(page, pageSize), wrapper);
//        page1.getRecords().forEach(item -> {
//            Classes classes = classesService.getById(item.getCid());
//            item.setCname(classes.getCname());
//        });
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
}
